package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    /**
     * @description: 使用REST风格信息信息
     * @param: name:姓名
     * @param: age:年龄
     * @param: sex:性别
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 10:10
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData1(@PathVariable(value = "name",required = true) String name,
                                @PathVariable(value = "age",required = true) Integer age,
                                @PathVariable(value = "sex",required = true) String sex){
        //上传参数检查
        //年龄不能为空
        if(age<=0){
            return AjaxResult.error("上传年龄异常");
        }
        //性别不能为空
        if(!"男".equals(sex) && !"女".equals(sex)){
            return AjaxResult.error("上传性别类型错误");
        }
        //构造参数,进行数据新增
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        reviewService.saveData(review);
        return AjaxResult.success("成功插入:1条数据");
    }
    @PostMapping("/saveData")
    public AjaxResult saveData2(@RequestBody MybatisReviewVO mybatisReviewVO){
        //参数有效性检查
        //姓名不能为空
        if(null == mybatisReviewVO.getName()){
            return AjaxResult.error("上传姓名为null");
        }
        //年龄不能为空
        if(null == mybatisReviewVO.getAge()){
            return AjaxResult.error("上传年龄为null");
        }
        if(mybatisReviewVO.getAge()<=0){
            return AjaxResult.error("上传年龄异常");
        }
        //性别不能为空
        if(null == mybatisReviewVO.getSex()){
            return AjaxResult.error("上传性别为Null");
        }
        if(!"男".equals(mybatisReviewVO.getSex()) && !"女".equals(mybatisReviewVO.getSex())){
            return AjaxResult.error("上传性别类型错误");
        }
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO,review);
        reviewService.saveData(review);
        return AjaxResult.success("成功插入:1条数据");
    }

    /**=========================================================删除数据=============================================*/
    /**
     * @description: 删除用户信息
     * @param: id :删除用户信息ID
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 11:54
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult deleteInfo(@PathVariable(value = "id",required = true) Long id){
        reviewService.deleteInfoById(id);
        return AjaxResult.success("删除成功:1条数据");
    }

    /**
     * @description: 批量删除用户信息
     * @param: list:用户编号ID
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 14:27
     */
    @PostMapping("/removeByList")
    public AjaxResult deleteByList(@RequestBody List<Long> list){
        if(null == list || list.size()==0){
            return AjaxResult.error("上传信息为空,无法查询");
        }
        List<Review> reviewList = reviewService.findInfoByList(list);
        if(reviewList.size() != list.size()){
            return AjaxResult.error("上传信息中含有无效信息");
        }
        reviewService.deleteByList(list);
        return AjaxResult.success("批量删除成功");
    }
    /**=========================================================修改数据=============================================*/
    /**
     * @description: 更新用户信息
     * @param: mybatisReviewVO 用户信息
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 11:07
     */
    @PostMapping("/update")
    public AjaxResult updateInfo(@RequestBody MybatisReviewVO reviewVO){
        //上传字段有效性检查-ID 不能为空
        if(null == reviewVO.getId()){
            return AjaxResult.error("用户信息中ID为Null");
        }
        //检查记录更新记录是否存在
        Review review = reviewService.findInfoById(reviewVO.getId());
        if(null ==review){
            return AjaxResult.error("用户信息更新信息不存在");
        }
        //更新记录
        BeanUtils.copyProperties(reviewVO,review);
        int result =reviewService.updateInfo(review);
        if(result>0){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }
    /**
     * @description: 通过用户编号查询用户信息
     * @param: id:用户编号
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 13:51
     */
    @GetMapping("/getById")
    public AjaxResult findInfoById(@RequestParam("id") Long id){
        Review review = reviewService.findInfoById(id);
        MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();
        BeanUtils.copyProperties(review,mybatisReviewVO);
        return AjaxResult.success(mybatisReviewVO);
    }
    /**
     * @description: 分页查询用户信息
     * @return: com.huike.common.core.page.TableDataInfo
     * @author: ShaJianBao
     * @date: 2022/9/1 14:03
     */
    @GetMapping("/getDataByPage")
    public TableDataInfo findInfoByPage(){
        super.startPage();
        List<Review> list =reviewService.findInfoByPage();
        List<MybatisReviewVO> reviewVOS = list.stream().map(item -> {
            MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();
            BeanUtils.copyProperties(item, mybatisReviewVO);
            return mybatisReviewVO;
        }).collect(Collectors.toList());
        return getDataTable(reviewVOS);
    }
    /**=========================================================查询数据=============================================*/
    /**
     * @description: 批量查询用户及信息
     * @param: list :用户编号批量信息
     * @return: com.huike.common.core.page.TableDataInfo
     * @author: ShaJianBao
     * @date: 2022/9/1 14:56
     */
    @GetMapping("/getDataByList")
    public TableDataInfo findInfoByList(@RequestParam List<Long> list){
        List<Review> reviewList = reviewService.findInfoByList(list);
        List<MybatisReviewVO> reviewVOList = reviewList.stream().map(item -> {
            MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();
            BeanUtils.copyProperties(item, mybatisReviewVO);
            return mybatisReviewVO;
        }).collect(Collectors.toList());
        return getDataTable(reviewVOList);
    }
    /**
     * @description: 动态更新用户信息
     * @param: mybatisReviewVO :用户信息
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 15:04
     */
    @PostMapping("/dynamicUpdate")
    public AjaxResult updateDynamicInfo(@RequestBody MybatisReviewVO mybatisReviewVO){
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO,review);
        reviewService.updateDynamicInfo(review);
        return AjaxResult.success("动态更新成功");
    }
    /**
     * @description:  批量插入用户信息
     * @param: list :用户信息
     * @return: com.huike.common.core.domain.AjaxResult
     * @author: ShaJianBao
     * @date: 2022/9/1 15:16
     */
    @PostMapping("/saveDataList")
    public AjaxResult saveDataList(@RequestBody List<MybatisReviewVO> list){
        if(null == list){
            return AjaxResult.error("上传的用户信息为空");
        }
        List<Review> reviewList = list.stream().map(item -> {
            Review review = new Review();
            BeanUtils.copyProperties(item, review);
            return review;
        }).collect(Collectors.toList());
        reviewService.saveDataList(reviewList);
        return AjaxResult.success("批量插入成功");
    }



}